import java.util.ArrayList;

public class Board {

	//Storleken.
	public int boardLength = 8;	
	
	//Public variables
	public char[][] thisBoard = new char[boardLength][boardLength];
	//Father board, the board that this board originates from, needed for one player
	public Board parentBoard;
	//What move that created this board
	public Coordinate madeMove;
	//who will play on this board
	public char plays;
	//What possible moves that exist on this board for the current player.
	public ArrayList<Coordinate> possibleMoves = new ArrayList<Coordinate>(1);
	//If someone has won
	public boolean won = false;
	//Who has won.
	public char wonColor;
	
	
	//Constructor for the first board
	public Board(){
		//initiates an empty board
		for(int i = 0 ; i < boardLength ; i++){
			for(int u = 0 ; u < boardLength ; u++){
				thisBoard[i][u] = 'E';
			}
		}
		//Places the first 4 pawns
		thisBoard[boardLength/2][boardLength/2] = 'B';
		thisBoard[boardLength/2 - 1][boardLength/2 - 1] = 'B';
		thisBoard[boardLength/2 - 1][boardLength/2] = 'W';
		thisBoard[boardLength/2][boardLength/2 - 1] = 'W';
		
		//White starts playing, no prior moves has been made
		plays = 'W';
		parentBoard = null;
		madeMove = null;
		
		//Calculates all possible moves
		checkMoves();
	}
	
	
	//Constructor for a new board
	public Board(Board oldBoard, Coordinate move){
		//Gives the board of the father
		thisBoard = oldBoard.thisBoard;
		madeMove = move;
		//gives the opponents colour
		if(oldBoard.plays == 'W'){
			plays = 'B';
		}else{
			plays = 'W';
		}
		//Makes the new move on the old board.
		makeMove(madeMove, plays);
		//Calculates all possible moves.
		checkMoves();
		//If no possible moves exists the game is over and we calculate who has won.
		if(possibleMoves.isEmpty()){
			whoWon();
			won = true;
		}
	}
	
	
	//Makes a move
	public void makeMove(Coordinate move, char moveColor){
		
		//Only allowed if the position is empty
		if(thisBoard[move.x][move.y] == 'E'){
			//Boolean for keeping track of if any pawns has been turned
			boolean turned = false;
			
			//Loopes 
			for(int i = -1 ; i < 2 ; i++){
				for(int u = - 1 ; u < 2 ; u++){
					if(this.checkDirection(move, i, u, moveColor)){
						this.turnPawns(move, i, u, moveColor);
						turned = true;
					}
				}
			}
			if(turned){
				thisBoard[move.x][move.y] = moveColor;
				if(plays == 'W'){
					plays = 'B';
				}else{
					plays = 'W';
				}	
			}
		}
		

	}
	
	public boolean checkPossible(Coordinate move, char moveColor){
		//Boolean for keeping track of if any pawns has been turned
		boolean turned = false;
		//Only allowed if the position is empty

		if(thisBoard[move.x][move.y] == 'E'){
			
			
			
			//Loopes 
			for(int i = -1 ; i < 2 ; i++){
				for(int u = - 1 ; u < 2 ; u++){
					if(this.checkDirection(move, i, u, moveColor)){
						turned = true;
					}
				}
			}

			
		}
		return turned;	
	}
	
	private boolean checkDirection(Coordinate inMove, int dirX, int dirY, char moveColor){
		if(dirX == 0 && dirY == 0){
			return false;
		}
		Coordinate move = new Coordinate(inMove);
		char opponentColor;
		boolean found = false;
		if(moveColor == 'B'){
			opponentColor = 'W';
		}else{
			opponentColor = 'B';
		}

		move.x += dirX;
		move.y += dirY;
		
		while(move.x >= 0 && move.x < boardLength && move.y >= 0 && move.y < boardLength && thisBoard[move.x][move.y] == opponentColor){
			found = true;
			move.x += dirX;
			move.y += dirY;
		}
		if(move.x < 0 || move.x >= boardLength || move.y < 0 || move.y >= boardLength){
			return false;
		}else if(thisBoard[move.x][move.y] == 'E'){
			return false;
		}else if(!found){
			return false;
		}else{
			return true;
		}
	}
	
	private void turnPawns(Coordinate inMove, int dirX, int dirY, char moveColor){
		if(dirX == 0 && dirY == 0){
			return;
		}
		
		Coordinate move = new Coordinate(inMove);
		char opponentColor;
		
		if(moveColor == 'B'){
			opponentColor = 'W';
		}else{
			opponentColor = 'B';
		}
		move.x += dirX;
		move.y += dirY;
		while(thisBoard[move.x][move.y] == opponentColor){
			thisBoard[move.x][move.y] = moveColor;
			move.x += dirX;
			move.y += dirY;
		}
	}
	
	public void showboard(){
		for(int i = 0 ; i < boardLength ; i++){
			for(int u = 0 ; u < boardLength ; u++){
				System.out.printf("%c", thisBoard[i][u]);
			}
			System.out.printf("\n");
		}
	}

	
	private void checkMoves(){
		
		for(int x = 0 ; x < boardLength ; x++){
			for(int y = 0 ; y < boardLength ; y++){
				boolean found = false;
				Coordinate current = new Coordinate(x,y);
				for(int i = -1 ; i < 2 ; i++){
					for(int u = - 1 ; u < 2 ; u++){
						if(this.checkDirection(current, i, u, plays)){
							found = true;
						}
					}
				}
				if(found){
					possibleMoves.add(current);
				}
			}
		}
	}
	
	private void whoWon(){
		int black = 0;
		int white = 0;
		
		for(int i = 0 ; i < boardLength ; i++){
			for(int u = 0 ; u < boardLength ; u++){
				if(thisBoard[i][u] == 'B'){
					black++;
				}else if(thisBoard[i][u] == 'W'){
					white++;
				}
			}
		}
		if(white>black){
			wonColor = 'W';
		}else if(white == black){
			wonColor = 'E';
		}else{
			wonColor = 'B';
		}
		
	}
	
	public Board makeChild(int moveX, int moveY){
		
		Coordinate move = new Coordinate(moveX,moveY);
		if(checkPossible(move, this.plays)){
			System.out.println("giltigt");
			return new Board(this, move);
		}else{
			System.out.println("inte giltigt");
			return this;
		}
	}

	
}




